angular.module('inspinia').controller('agentActivityDataCtrl',function($scope,$http,i18nService,SweetAlert,$document,$stateParams,$state){
    i18nService.setCurrentLang('zh-cn');  //设置语言为中文
    $scope.paginationOptions=angular.copy($scope.paginationOptions);
    $scope.paginationOptions2=angular.copy($scope.paginationOptions);

    $scope.initInfo=function(){
        $http.post("agentActivityAction/getAgentActivity", "actCode="+$stateParams.actCode,
            {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .success(function(data){
                if(data.status){
                    var info=data.info;
                    $scope.info1={actCode:$stateParams.actCode,actName:info.actName,actType:"1",agentNo:""};
                    $scope.info2={actCode:$stateParams.actCode,actName:info.actName,actType:"2",agentNo:""};
                }
            });
    }
    $scope.initInfo();

    //清空
    $scope.clear=function(sta){
        if(sta==1){
            $scope.info1.agentNo="";
        }else{
            $scope.info2.agentNo="";
        }
    };

    //清空
    $scope.clear=function(sta){
        if(sta==1){
            $scope.info1.agentNo="";
        }else{
            $scope.info2.agentNo="";
        }
    };

    $scope.userGrid={                           //配置表格
        data: 'result',
        paginationPageSize:10,                  //分页数量
        paginationPageSizes: [10,20,50,100],	//切换每页记录数
        useExternalPagination: true,		    //开启拓展名
        enableHorizontalScrollbar: true,        //横向滚动条
        enableVerticalScrollbar : true,  		//纵向滚动条
        columnDefs:[                           //表格数据
            { field: 'agentNo',displayName:'代理商编号',width:180},
            { field: 'agentName',displayName:'代理商名称',width:180 },
            { field: 'agentLevel',displayName:'代理商级别',width:180 },
            { field: 'agentNode',displayName:'代理商节点',width:180 },
            { field: 'oneAgentNo',displayName:'一级代理商编号',width:180 },
            { field: 'oneAgentName',displayName:'一级代理商名称',width:180 },
            { field: 'oneSaleName',displayName:'一级代理商所属销售',width:180 },
            { field: 'totalMersStr',displayName:'交易商户数',width:180 },
            { field: 'totalTransStr',displayName:'累计商户交易额',width:180 },
            { field: 'totalActMersStr',displayName:'累计活动商户数',width:180 },
            { field: 'totalAverageTrans',displayName:'活动商户数人均',width:180 },
            { field: 'totalActTransStr',displayName:'累计活动商户交易额',width:180 },
            { field: 'totalNoActMers',displayName:'未参与商户数',width:180 },
            { field: 'totalAverageNoActTrans',displayName:'未参与商户人均',width:180 },
            { field: 'score',displayName:'牛气值',width:180}
        ],
        onRegisterApi: function(gridApi) {
            $scope.gridApi = gridApi;
            gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                $scope.paginationOptions.pageNo = newPage;
                $scope.paginationOptions.pageSize = pageSize;
                $scope.query(1);
            });
        }
    };
    $scope.userGrid2={                           //配置表格
        data: 'result2',
        paginationPageSize:10,                  //分页数量
        paginationPageSizes: [10,20,50,100],	//切换每页记录数
        useExternalPagination: true,		    //开启拓展名
        enableHorizontalScrollbar: true,        //横向滚动条
        enableVerticalScrollbar : true,  		//纵向滚动条
        columnDefs:[                           //表格数据
            { field: 'agentNo',displayName:'代理商编号',width:180},
            { field: 'agentName',displayName:'代理商名称',width:180 },
            { field: 'agentLevel',displayName:'代理商级别',width:180 },
            { field: 'agentNode',displayName:'代理商节点',width:180 },
            { field: 'oneAgentNo',displayName:'一级代理商编号',width:180 },
            { field: 'oneAgentName',displayName:'一级代理商名称',width:180 },
            { field: 'oneSaleName',displayName:'一级代理商所属销售',width:180 },
            { field: 'totalMersStr',displayName:'交易商户数',width:180 },
            { field: 'totalTransStr',displayName:'累计商户交易额',width:180 },
            { field: 'totalActMersStr',displayName:'累计活动商户数',width:180 },
            { field: 'totalAverageTrans',displayName:'活动商户数人均',width:180 },
            { field: 'totalActTransStr',displayName:'累计活动商户交易额',width:180 },
            { field: 'totalNoActMers',displayName:'未参与商户数',width:180 },
            { field: 'totalAverageNoActTrans',displayName:'未参与商户人均',width:180 },
            { field: 'score',displayName:'壕气值',width:180},
            { field: 'joinRatio',displayName:'当前参与率',width:180}
        ],
        onRegisterApi: function(gridApi) {
            $scope.gridApi2 = gridApi;
            gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                $scope.paginationOptions2.pageNo = newPage;
                $scope.paginationOptions2.pageSize = pageSize;
                $scope.query(2);
            });
        }
    };

    $scope.query=function(sta){
        if ($scope.loadImg) {
            return;
        }
        $scope.loadImg = true;
        var data;
        if(sta==1){
            data = {
                info:angular.toJson($scope.info1),
                pageNo:$scope.paginationOptions.pageNo,
                pageSize:$scope.paginationOptions.pageSize
            };
        }else{
            data = {
                info:angular.toJson($scope.info2),
                pageNo:$scope.paginationOptions2.pageNo,
                pageSize:$scope.paginationOptions2.pageSize
            };
        }
        $http.post("agentActivityAction/selectAgentActivityData",data,$scope.postCfgAllCurrency)
            .success(function (data) {
                if (data.status) {
                    if(sta==1){
                        $scope.result=data.page.result;
                        $scope.userGrid.totalItems = data.page.totalCount;
                    }else{
                        $scope.result2=data.page.result;
                        $scope.userGrid2.totalItems = data.page.totalCount;
                    }
                } else {
                    $scope.notice(data.msg);
                }
                $scope.loadImg = false;
            }).error(function(data){
                $scope.notice(data.msg);
                $scope.loadImg = false;
            });
    };
    //导出
    $scope.exportExcel=function(sta){
        SweetAlert.swal({
                title: "确认导出？",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "提交",
                cancelButtonText: "取消",
                closeOnConfirm: true,
                closeOnCancel: true },
            function (isConfirm) {
                if (isConfirm) {
                    var data;
                    if(sta==1){
                        data=angular.toJson($scope.info1);
                    }else{
                        data=angular.toJson($scope.info2);
                    }
                    $scope.exportInfoClick("agentActivityAction/exportActivityData",{"info" :data});
                }
            });
    };

    $scope.toBack=function(){
        $state.transitionTo('agent.agentActivityQuery',null,{reload:true});
    }
    //页面绑定回车事件
    $document.bind("keypress", function(event) {
        $scope.$apply(function (){
            if(event.keyCode == 13){
                $scope.query();
            }
        })
    });
});